package cn.schoolwow.quickdao.module.database.ddl.query.flow.property.getProperty;

import cn.schoolwow.quickdao.domain.entity.Property;
import cn.schoolwow.quickdao.module.database.parent.domain.ResultSetConsumer;
import cn.schoolwow.quickdao.module.database.parent.flow.executor.ExecuteQueryConnectionFlow;
import cn.schoolwow.quickflow.domain.FlowContext;
import cn.schoolwow.quickflow.flow.BusinessFlow;

import java.sql.ResultSet;
import java.util.Arrays;

public class GetPropertyByOracleFlow implements BusinessFlow {
    @Override
    public void executeBusinessFlow(FlowContext flowContext) throws Exception {
        String tableName = (String) flowContext.checkData("tableName");
        String columnName = (String) flowContext.checkData("columnName");

        flowContext.startFlow(new ExecuteQueryConnectionFlow())
                .putTemporaryData("name", "获取指定表字段信息")
                .putTemporaryData("sql", "select column_type, nullable, data_length from user_tab_columns where table_name = ? and column_name = ?")
                .putTemporaryData("parameters", Arrays.asList(tableName, columnName))
                .putReturnData("resultSetConsumer",new ResultSetConsumer() {
                    @Override
                    public void consumeResultSet(ResultSet resultSet) throws Exception {
                        if(resultSet.next()){
                            Property property = new Property();
                            property.column = resultSet.getString("column_name");
                            property.columnType = resultSet.getString("column_type");
                            if(property.columnType.contains(" ")){
                                property.columnType = property.columnType.substring(0,property.columnType.indexOf(" "));
                            }
                            String dataLength = resultSet.getString("data_length");
                            if(property.columnType.toLowerCase().contains("char")&&null!=dataLength&&!dataLength.isEmpty()){
                                property.columnType += "(" + dataLength + ")";
                            }
                            property.notNull = "N".equals(resultSet.getString("nullable"));
                            flowContext.putData("property", property);
                        }
                    }
                })
                .execute();

        if(flowContext.containKey("property")){
            flowContext.broken("字段不存在");
        }
        Property property = (Property) flowContext.checkData("property");

        flowContext.startFlow(new ExecuteQueryConnectionFlow())
                .putTemporaryData("name", "获取指定表字段注释")
                .putTemporaryData("sql", "select comments from user_col_comments where table_name = ? and column_name = ?")
                .putTemporaryData("parameters", Arrays.asList(tableName, columnName))
                .putReturnData("resultSetConsumer",new ResultSetConsumer() {
                    @Override
                    public void consumeResultSet(ResultSet resultSet) throws Exception {
                        if(resultSet.next()){
                            property.comment = resultSet.getString("comments");
                        }
                    }
                })
                .execute();
    }

    @Override
    public String name() {
        return "获取数据库字段-Oracle数据库";
    }
}
